<template>
    <basic-container>
    <el-form ref="form" :model="form" label-width="80px">
        <div v-for="(item, index) in objA" :key="index">
            <el-form-item :label="item.organizationName">
                <el-input style="width: 300px" @blur="checkPhone(item.phone)" v-model="item.phone"></el-input>
            </el-form-item>
        </div>
        <el-form-item>
            <el-button type="primary" :loading="setPhoneLoad" @click="submitA">提 交</el-button>
        </el-form-item>
    </el-form>
    </basic-container>
</template>
<script>
    import{
        getPhone,
        setPhone
    } from "../../api/base/sysSet/phoneSet";
    import { mapGetters } from "vuex";
    export default {
        data() {
            return {
                objA:{},
                setPhoneLoad:false,
            }
        },
        //页面初始化
        created(){
            this.getData()
        },
        computed: {
            ...mapGetters(["permission", 'userInfo']),
        },
        methods: {
            getData(){
                getPhone().then(res=>{
                    if(res.data.code==200){
                        this.objA=res.data.data
                    }
                    else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //输入框失去焦点事件
            checkPhone(phone){
                if(phone){
                    let re = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
                    let flag = false;
                    flag = re.test(phone)
                    if (!flag) {
                        return  this.$message.error("输入的手机号码格式不正确");
                    }
                }
            },
            submitA () {
                this.setPhoneLoad = true;
                console.log(this.objA);
                let re = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
                if(this.objA.length>0){
                    for(let i=0;i<this.objA.length;i++){
                        if(this.objA[i].phone){
                            let isPass = false;
                            isPass = re.test(this.objA[i].phone)
                            if(!isPass){
                                this.$message.error("输入的手机号码格式不正确");
                            }
                        }
                    }
                }
                setPhone(this.objA).then(res=>{
                    this.setPhoneLoad = false;
                    if(res.data.code==200){
                        this.$message({
                            message:'提交成功',
                            type:'success'
                        })
                        this.getData()
                    }
                    else {
                        this.setPhoneLoad = false;
                        this.$message.error(res.data.message);
                    }
                })
            },
        }
    };
</script>
<style>
    h5{
        color: darkred;
        font-weight: bold;
    }
    p{
        line-height: 30px;
    }

</style>